define(["PDUtilDir/grid","PDUtilDir/searchBlock","PDUtilDir/slidebar", "PDUtilDir/tool","PDUtilDir/util","PDUtilDir/inputSelect",  "jquery", "Date","DateCN", "css!DateCss"],
		function(Grid,SearchBlock,Slidebar,Tool,Util,inputSelect){
	
	var workTypeData = {};
	var workContentData = {};
	var projectData = {};
	
	/**
	 * 页面展示初始化
	 */
	 var init = function(){
		 //初始化查询框样式
	        SearchBlock.init('searchBlockId');
	      //初始化页面上的下拉框数据
	        initInputSelect();
	        
	        
	   //     timePlugsInit();
	        initGrid();
	       
	     
	    }
	 
	var initInputSelect = function(){
		initProjectName("queryProjectName","queryProjectId",null);
		initWorkType("queryWorkType","queryWorkTypeCode",null);
		initWorkContent("queryWorkContent","queryWorkContentCode",null);
	}
	var initProjectName = function(id,showLable,idata){
		$.ajax({
			url:getServer()+"/flea/project/queryAll.htmls",
			success:function(data){
				projectData = data;
			    projectSelect(id,showLable,idata);
			}
		});
	}
    var projectSelect = function(id,showLable,idata){
    	inputSelect({
    		id:id,
    		key : {
    			id : "projectId",
    			name : "projectName",
    			data : "data",
    		},
    		data :projectData,
/*    		data :dataSimple,
*/    		simpleData : true,
    		initData:idata,
    		//启用文本搜索
    		searchAble : true,
    		//选择之后回调方法：id，选择的id数组，data，选择的数据数组
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n.projectId;
    			})
    			$("#"+showLable).val(str);
    		}
    	});
    }

	var initWorkType = function(id,showLable,idata){
		$.ajax({
			url:getServer()+"/flea/dict/queryTreeChild.htmls",
			data:{
				"dictCode":"WK_TYPE"
			},
			success:function(data){
				workTypeData = data;
			    initTypeSelect(id,showLable,idata);
			}
		});
	}
	
	 var initTypeSelect = function(id,showLable,idata){
	        inputSelect({
	            id:id,
	            key : {
	                id : "dictCode",
	                name : "dictName",
	                data : "data",
	            },
	            data :workTypeData,
	            simpleData : true,
	            initData:idata,
	            //启用文本搜索
	            searchAble : true,
	            //选择之后回调方法：id，选择的id数组，data，选择的数据数组
	            callback : function(id, data) {
	                var str = "";
	                $.each(data, function(i, n){
	                    str += n.dictCode;
	                })
	                $("#"+showLable).val(str);
	            }
	        });
	    }
	
	 var initWorkContent = function(id,showLable,idata){
			$.ajax({
				url:getServer()+"/flea/dict/queryTreeChild.htmls",
					data:{
					 "dictCode":"WKCONTENTTYPE"
				 },
				 success:function(data){
					 workContentData = data;
						initContentTypetSelect(id,showLable,idata);
				 }
			});
		}
	
	var initContentTypetSelect = function(id,showLable,idata){
	 	inputSelect({
    		id:id,
    		key : {
    			id : "dictCode",
    			name : "dictName",
    			data : "data",
    		},
    		data :workContentData,
    		simpleData : true,
    		initData:idata,
    		//启用文本搜索
    		searchAble : true,
    		//选择之后回调方法：id，选择的id数组，data，选择的数据数组
    		callback : function(id, data) {
    			var str = "";
    			$.each(data, function(i, n){
    				str += n.dictCode;
    			})
    			$("#"+showLable).val(str);
    		}
    	});
	}
	var initEditData = function(idata){
		
		initProjectName("projectName","projectId",idata);
		initWorkType("workType","workTypeCode",idata);
		initWorkContent("workContent","workContentCode",idata);
		initStatus(idata);
	}
	 var initStatus = function(idata){
		 var jobStatus = [{"dictCode":"0","dictName":"未发布"},{"dictCode":"1","dictName":"发布"}];
		 
	        inputSelect({
	            id:"jobStatusCode",
	            key : {
	                id : "dictCode",
	                name : "dictName",
	                data : "data",
	            },
	            data :jobStatus,
	            simpleData : true,
	            initData:idata,
	            //启用文本搜索
	            searchAble : true,
	            //选择之后回调方法：id，选择的id数组，data，选择的数据数组
	            callback : function(id, data) {
	                var str = "";
	                $.each(data, function(i, n){
	                    str += n.dictCode;
	                })
	                $("#jobStatus").val(str);
	            }
	        });
	    }
	
	
    var slidebar ={};
    var editJob = function(data){
    	slidebar = Slidebar({
    		url : getServer() + "/static/app/project/workJob/view/editJob.html",
//    		close:true,
    		afterLoad : function($Panel) {
    			initEditData(data);
    			 //绑定保存时间按钮
    	    	$("#btnSave").on("click",function() {
    	    		saveJob();
    	    	});
    	    	if(null!=data){

    	    		$("#projectName").val(data.projectName);
    	    		$("#workType").val(data.workType);
    	    		$("#workContent").val(data.workContent);
    	    		if(data.status==0){
    	    			$("#jobStatusCode").val("未发布");
    	    		}else{
    	    			$("#jobStatusCode").val("已发布");
    	    		}
    	    	
    	    	
    	    		Tool.deserialize(("jobForm"), data);
    	    		
    	    	}
    		}
    	})
    }
	
	var saveJob = function(){

		var jobForm = Tool.serialize("jobForm"); //序列化表单
		$.ajax({
			url:getServer()+"/flea/workJob/save.htmls",
			data:
			{"workJob":JSON.stringify(jobForm)},
			success:function(data){
				Util.alert("操作成功");
				grid.refresh();
				slidebar.close();
			}
		});
	
	}	
	
	
var getQueryParam = function(){
	var param = {
			projectId:$("#queryProjectId").val(),
			workTypeCode:$("#queryWorkTypeCode").val(),
			workContentCode:$("#queryWorkContentCode").val(),
			jobName:$("#queryJobName").val(),
	}
	return param;
}
var grid = {};
    var initGrid =  function(param){
    	grid =  Grid({
            id:"joblist",
            pageSize:10,
            index:true,
            multi:true,
            pagination : true,
            cache:false,
            layout:[
                {name:"任务名称",field:"workJobName",click:function(e){
                	var data = e.data.row;
                	editJob(data);
                }},
                {name:"工作类别",field:"workType",width:"10%"},
                {name:"工作内容",field:"workContent",width:"10%"},
                {name:"项目名称",field:"projectName",width:"10%"},
                {name:"是否启用",field:"status",format:function(obj){
                	if(obj.row.status==0){
                		return "未发布";
                	}else{
                		  return  "已发布";
                	}
                  
                }},
                {name:"创建时间",field:"createTime"},
                {name:"最后修改时间",field:"lastModifyTime"}
            ],
            toolbar:[
                {name:"添加",icon:"fa fa-plus-circle",callback:function(event){
                	editJob(null);
                }},
                {name:"发布",icon:"fa fa-arrows",callback:function(event){
                	var flag=1;
                	pubulish(flag);
                }},
                {name:"取消发布",icon:"fa fa-arrows",callback:function(event){
                	var flag=0;
                	pubulish(flag);
                }},
                {name:"查询",icon:"fa fa-search",callback:function(event){
                	initGrid(getQueryParam());
                }},
                {name:"选择",icon:"fa fa-search",callback:function(event){
                	selectJob();
                }}
            ],
            data:{ type:"URL",value:getServer()+"/flea/workJob/findAllForGrid.htmls"},
            queryParam:param
        });
    }
    var pubulish = function(flag){
    	var row = grid.getSelectedRow();
    	
    	if(row.length==0){
    		Util.alert("请选择要操作的数据");
    		return;
    	}
    	var ids= new Array();
    	for (var i = 0; i < row.length; i++){
    		ids.push(row[i].workJobId);
    	}
    		$.ajax({
        		type:"post",
        		url:getServer()+"/flea/workJob/updateStatus.htmls",
        		data:{"ids":JSON.stringify(ids),"flag":flag},
        		success:function(data){
        			if(data.success)
        				Util.alert(data.message)
        		}
        	})
    }
    var selectJob=function(){
    	var row=grid.getSelectedRow();
    	if(row.length==0){
    		Util.alert("请选择要操作的数据");
    	}
    	var ids=new Array();
    	for(var i=0;ids<row.length;i++){
    		ids.push(row[i].workJobId);
    	}
    	// console.log(ids);
    	$.ajax({
    		url:getServer()+"/flea/workJob/createApply.htmls",
    		data:{"ids":JSON.stringify(ids)},
    		succuss:function(data){
    			if(data.succuss){
    				Util.alert(data.message);
    			}
    		}
    	})
    }
    return {
        init:init
    }
});